﻿using System;

namespace Syufy.BINLookup.Web.Helpers
{
    /// <summary>
    /// Extension methods for .NET primitive types
    /// </summary>
    public static class PrimitiveExtensions
    {
        /// <summary>
        /// Handle null strings and return a default value.
        /// </summary>
        /// <param name="currentValue">Current string value.</param>
        /// <param name="defaultValue">Value to return if null.</param>
        /// <returns>String value.</returns>
        public static string ReturnDefaultIfNull(this string currentValue, string defaultValue)
        {
            return string.IsNullOrEmpty(currentValue) ? defaultValue : currentValue;
        }

        /// <summary>
        /// Handle MinDate and return a default value.
        /// </summary>
        /// <param name="currentValue">Current date value.</param>
        /// <param name="defaultValue">Value to return if MinDate.</param>
        /// <returns>DateTime value.</returns>
        public static DateTime ReturnDefaultIfMinDate(this DateTime currentValue, DateTime defaultValue)
        {
            return DateTime.MinValue.Equals(currentValue) ? defaultValue : currentValue;
        }

        /// <summary>
        /// Handle null decimal and return a default nullable value.
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal? ReturnDefaultIfNullableDecimal(this decimal? currentValue, decimal? defaultValue)
        {
            return !currentValue.HasValue ? defaultValue : currentValue.Value;
        }

        /// <summary>
        /// Handle null decimal and return a default nullable value.
        /// </summary>
        /// <param name="currentValue"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int ReturnDefaultIfDefaultInt(this int currentValue, int defaultValue)
        {
            return currentValue == default(int) ? defaultValue : currentValue;
        }
    }
}